package com.cloud.code.牛客网;

import org.junit.Test;

import java.util.*;

public class Test31 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            int n = Integer.parseInt(scanner.nextLine());
            String[] strings = new String[n];
            Map<String, Integer> map = new LinkedHashMap<>();
            for (int i = 0; i < n; i++) {
                String nextLine = scanner.nextLine();
                Integer integer = doChange(nextLine);
                map.put(nextLine, integer);
            }

            List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());
            list.sort(new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                    return o1.getValue() - o2.getValue();
                }
            });

            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getKey());
            }


        }
    }

    private static Integer doChange(String nextLine) {

        String s = nextLine.toUpperCase(Locale.ROOT);
        String[] split = s.split("[A-Z]");
        int leng = 0;

        for (int i = 0; i < split.length; i++) {
            leng = split[i].length() + leng;
            String sub = s.substring(leng, leng + 1);

            int sum = 0;
            switch (sub) {
                case "M":
                    sum = sum + Integer.parseInt(split[i]);
                    break;
                case "G":
                    sum = sum + Integer.parseInt(split[i]) * 1024;
                    break;
                case "T":
                    sum = sum + Integer.parseInt(split[i]) * 1024 * 1024;
                    break;
            }

            return sum;

        }

        return 1;

    }

    @Test
    public void test() {
        String str = "10M10G";
        String[] split = str.split("[A-Z]");

        System.out.println(split);
    }
}
